package com.baozheyangken.ai_learn.controller;

import com.baozheyangken.ai_learn.annotation.RequiresAuthentication;
import com.baozheyangken.ai_learn.pojo.entity.Course;
import com.baozheyangken.ai_learn.pojo.vo.CourseVO;
import com.baozheyangken.ai_learn.service.CourseService;
import com.baozheyangken.ai_learn.service.serviceImpl.CourseServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/courses")
@RequiresAuthentication
public class CourseController {
    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    private CourseService courseService;

    // 创建课程
    @PostMapping
    public ResponseEntity<Course> createCourse(@RequestBody CourseVO courseVO) {
        try {
            Course createdCourse = courseService.createCourse(courseVO);
            return new ResponseEntity<>(createdCourse, HttpStatus.CREATED);
        } catch (Exception e) {
            logger.error("创建课程失败，请求参数: {}", courseVO, e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 获取所有课程
    @GetMapping
    public ResponseEntity<List<Course>> getAllCourses() {
        List<Course> courses = courseService.getAllCourses();
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }

    // 根据ID获取课程
    @GetMapping("/{id}")
    public ResponseEntity<Course> getCourseById(@PathVariable Integer id) {
        Course course = courseService.getCourseById(id);
        return new ResponseEntity<>(course, HttpStatus.OK);
    }

    // 更新课程
    @PutMapping("/{id}")
    public ResponseEntity<Course> updateCourse(@PathVariable Integer id, @RequestBody CourseVO courseVO) {
        try {
            Course updatedCourse = courseService.updateCourse(id, courseVO);
            return new ResponseEntity<>(updatedCourse, HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 删除课程
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCourse(@PathVariable Integer id) {
        try {
            courseService.deleteCourse(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 根据学科和年级筛选课程
    @GetMapping("/filter")
    public ResponseEntity<List<Course>> getCoursesBySubjectAndGrade(@Param("subject") String subject,
                                                                    @Param("grade") String grade) {
        List<Course> courses = courseService.getCoursesBySubjectAndGrade(subject, grade);
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }

    // 搜索课程
    @GetMapping("/search")
    public ResponseEntity<List<Course>> searchCourses(@RequestParam String keyword) {
        List<Course> courses = courseService.searchCourses(keyword);
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }
}